package com.dhcc.bpm.modules.desk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.cache.CacheNames;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.desk.entity.Notice;
import com.dhcc.bpm.modules.desk.service.INoticeService;
import com.dhcc.bpm.modules.desk.vo.NoticeVO;
import com.dhcc.bpm.modules.desk.wrapper.NoticeWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
/**
 * @ClassName NoticeController
 * @Description  通知公告 控制器
 * @Author YZC
 * @Date 2019/11/19 14:46
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@RequestMapping(AppConstant.APPLICATION_DESK_NAME + "/notice")
@AllArgsConstructor
@ApiSort(2)
@Api(value = "用户博客", tags = "博客接口")
public class NoticeController extends BladeController implements CacheNames {

	private INoticeService noticeService;

	/**
	 * @Description 通知公告详情
	 * @Author daina
	 * @Date 15:04 2020/3/26
	 * @param notice
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.desk.vo.NoticeVO>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "通知公告详情", notes = "传入notice")
	@ApiLog("通知公告详情")
	@Log(title = "通知公告详情", businessType = BusinessType.SELECT)
	public R<NoticeVO> detail(Notice notice) {
		Notice detail = noticeService.getOne(Condition.getQueryWrapper(notice));
		return R.data(NoticeWrapper.build().entityVO(detail));
	}

	/**
	 * @Description 通知公告分页
	 * @Author daina
	 * @Date 15:07 2020/3/26
	 * @param notice
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.desk.vo.NoticeVO>>
	 **/
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "category", value = "公告类型", paramType = "query", dataType = "integer"),
		@ApiImplicitParam(name = "title", value = "公告标题", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "通知公告分页", notes = "传入notice")
	@ApiLog("通知公告分页")
	@Log(title = "通知公告分页", businessType = BusinessType.SELECT)
    //@PreAuth("hasAnyRole('user','administrator')")
    //@PreAuth("hasPermission('desk:notice:page')")
	public R<IPage<NoticeVO>> list(@ApiIgnore @RequestParam Map<String, Object> notice, Query query) {
		IPage<Notice> pages = noticeService.page(Condition.getPage(query), Condition.getQueryWrapper(notice, Notice.class));
		return R.data(NoticeWrapper.build().pageVO(pages));
	}

	/**
	 * @Description 多表联合查询自定义分页
	 * @Author daina
	 * @Date 15:06 2020/3/26
	 * @param notice
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.desk.vo.NoticeVO>>
	 **/
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "category", value = "公告类型", paramType = "query", dataType = "integer"),
		@ApiImplicitParam(name = "title", value = "公告标题", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiLog("多表联合查询自定义分页")
	@Log(title = "多表联合查询自定义分页", businessType = BusinessType.SELECT)
	@ApiOperation(value = "多表联合查询自定义分页", notes = "传入notice")
	public R<IPage<NoticeVO>> page(@ApiIgnore NoticeVO notice, Query query) {
		IPage<NoticeVO> pages = noticeService.selectNoticePage(Condition.getPage(query), notice);
		return R.data(pages);
	}

	/**
	 * @Description 新增通知公告
	 * @Author daina
	 * @Date 15:06 2020/3/26
	 * @param notice
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增通知公告", notes = "传入notice")
	@ApiLog("新增通知公告")
	@Log(title = "新增通知公告", businessType = BusinessType.INSERT)
	public R save(@RequestBody Notice notice) {
		return R.status(noticeService.save(notice));
	}

	/**
	 * @Description 修改通知公告
	 * @Author daina
	 * @Date 15:06 2020/3/26
	 * @param notice
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改通知公告", notes = "传入notice")
	@ApiLog("修改通知公告")
	@Log(title = "修改通知公告", businessType = BusinessType.UPDATE)
	public R update(@RequestBody Notice notice) {
		return R.status(noticeService.updateById(notice));
	}
	/**
	 * @Description 修改通知公告
	 * @Author daina
	 * @Date 15:06 2020/3/26
	 * @param notice
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/clearInfo")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改通知公告", notes = "传入notice")
	@ApiLog("修改通知公告")
	@Log(title = "修改通知公告", businessType = BusinessType.UPDATE)
	public R clearInfo(@RequestBody NoticeVO notice) {
		List<Long> noticeIds = Func.toLongList(notice.getIds());
		Boolean bool = true;
		for (int i=0;i<noticeIds.size();i++){
			Notice noticeVo = new Notice();
			noticeVo.setId(noticeIds.get(i));
			bool = noticeService.updateNotice(noticeVo);
		}
		return R.status(bool);
	}
	/**
	 * @Description 新增或修改通知公告
	 * @Author daina
	 * @Date 15:05 2020/3/26
	 * @param notice
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改通知公告", notes = "传入notice")
	@ApiLog("新增或修改通知公告")
	@Log(title = "新增或修改通知公告", businessType = BusinessType.INSERT)
	public R submit(@RequestBody Notice notice) {
		return R.status(noticeService.saveOrUpdate(notice));
	}

	/**
	 * @Description 通知公告删除
	 * @Author daina
	 * @Date 15:05 2020/3/26
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "通知公告删除", notes = "传入notice")
	@ApiLog("通知公告删除")
	@Log(title = "通知公告删除", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
		boolean temp = noticeService.deleteLogic(Func.toLongList(ids));
		return R.status(temp);
	}

}
